package com.zoe.wan.android.example.fragment.knowledge

import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import com.zoe.wan.android.example.BR
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.activity.knowledge.KnowledgeDetialActivity
import com.zoe.wan.android.example.common.adapter.KnowledgeListAdapter
import com.zoe.wan.android.example.databinding.FragmentKnowledgeBinding
import com.zoe.wan.android.example.repositry.data.knowledge.DetailIntentData
import com.zoe.wan.android.example.repositry.data.knowledge.DetailIntentItemData
import com.zoe.wan.android.example.repositry.data.knowledge.KnowledgeListDataItem
import com.zoe.wan.base.BaseFragment

class FragKnowledge : BaseFragment<FragmentKnowledgeBinding, FragKnowledgeViewModel>() {

    private val adapter = KnowledgeListAdapter()
    override fun getLayoutId(): Int {
        return R.layout.fragment_knowledge
    }

    override fun getViewModelId(): Int {
        return BR.konwledgeVm
    }

    override fun initViewData() {
        binding?.recycle?.layoutManager = LinearLayoutManager(context)
        binding?.recycle?.adapter = adapter


        viewModel?.knowledgeListData?.observe(viewLifecycleOwner) {
            adapter.setDataList(it)
        }

        adapter.registerListener(object : KnowledgeListAdapter.KnowledgeCollectListener {


            override fun itemClick(item: KnowledgeListDataItem) {

                var list = mutableListOf<DetailIntentItemData>()

                item.children?.forEach {
                    val detail = DetailIntentItemData("${it?.id}",it?.name)
                    list.add(detail)
                }
                val model = DetailIntentData(list)

                val intent = Intent(context, KnowledgeDetialActivity::class.java)
                intent.putExtra(KnowledgeDetialActivity.INTENT_TAV_DATA,model)
//                intent.p
                startActivity(intent)
            }
        })

    }

}